package com.asiainfo.hybridapp.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.asiainfo.hybridapp.R;
import com.asiainfo.hybridapp.activity.BaseActivity;
import com.asiainfo.hybridapp.activity.CommonFullTBSWebViewActivity;
import com.asiainfo.hybridapp.activity.CommonFullWebViewActivity;
import com.asiainfo.hybridapp.activity.MonitorDetailActivity;
import com.asiainfo.hybridapp.configs.ConstantsME;
import com.asiainfo.hybridapp.configs.DebugConfig;
import com.asiainfo.hybridapp.configs.InterfaceConfig;
import com.asiainfo.hybridapp.configs.RealInterfaceConfig;
import com.asiainfo.hybridapp.entity.PopulationRankInfo;
import com.framework2.baseEvent.BaseOnClickListener;
import com.library.adapter_recyclerview.UniversalAdapter;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * 人口排行adapter
 *
 * @author YobertJomi
 * className PopulationRankAdapter
 * created at  2017/6/16  10:49
 */
public class PopulationRankAdapter extends UniversalAdapter<PopulationRankInfo> {
    private SimpleDateFormat simpleDateFormat;
    private Context context;

    public PopulationRankAdapter(@NonNull Context context, @LayoutRes int layoutId, @Nullable List list) {
        super(context, layoutId, list);
        this.context = context;
        if (null == simpleDateFormat) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        }
    }

    @Override
    protected void getItemView(UniversalViewHolder viewHolder, final PopulationRankInfo item, final int position) {
//                    SpannableString spannableString = new SpannableString("共" + info.getPhoneCount() + "件商品 合计：¥" +
//                    decimalFormat.format(info.getTotalFee()));
//                    spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color._toast_txt)),
//                    String.valueOf("共" + info.getPhoneCount() + "件商品 合计：¥").length(), spannableString.length(),
//                            Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//                    viewHolder.setText(R.id.tv_order_code, "订单编号:" + info.getOrderNo());
//                    try {
//                        String date = simpleDateFormat.format(new Date(item.getStockOutTime() * 1000));
//                        item.setDate(date);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
        viewHolder.setText(R.id.tvTitle, TextUtils.isEmpty(item.getTitle()) ? "title" : item.getTitle());
        viewHolder.setText(R.id.tvDate, TextUtils.isEmpty(item.getDate()) ? "date" : item.getDate());
        viewHolder.setText(R.id.tvCount, item.getCount() + "人");
        viewHolder.itemView.setOnClickListener(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {
                if (position == 0)
                    ((BaseActivity) context).startActivity(MonitorDetailActivity.class, item);
                else {
                    Bundle b = new Bundle();
                    b.putString(ConstantsME.title, item.getTitle());
                    b.putString(ConstantsME.url,
                            RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.webKPIdetailWithoutId + item.getId());
                    if (DebugConfig.TBSx5) {
                        ((BaseActivity) context).startActivity(CommonFullTBSWebViewActivity.class, b);
                    } else {
                        ((BaseActivity) context).startActivity(CommonFullWebViewActivity.class, b);
                    }
                }
            }
        });
    }
}
